/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.view.global;

import com.intelligent.ispc.client.core.view.LoginPanel;
import com.intelligent.ispc.client.core.view.analysis.grr.GrrMainPanel;
import com.intelligent.ispc.client.core.view.analysis.report.YieldPanel;
import com.intelligent.ispc.client.core.view.analysis.spc.SpcMainPanel;
import com.intelligent.ispc.client.core.view.system.ScriptTestPanel;
import com.intelligent.ispc.client.core.view.system.SystemConfigPanel;
import com.intelligent.ispc.client.core.view.configuration.AnalysisConfigPanel;
import com.intelligent.ispc.client.core.view.system.ChangePwdPanel;
import com.intelligent.ispc.client.event.PanelChangeEvent;
import com.intelligent.ispc.client.event.StatusBarMsgChangeEvent;
import com.intelligent.ispc.client.utils.AppMessageFactory;
import com.intelligent.ispc.client.utils.EventBusUtils;
import com.intelligent.ispc.client.utils.ResourceMessages;
import com.intelligent.ispc.common.utils.ResourceBundleUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Peter on 2016/7/13.
 * Updated by Tony.yu on 2016/11/2.
 */
@Component
public class MainTopMenuBar extends JMenuBar {

    /**
     * Constructor
     */
    public MainTopMenuBar() {
    }

    /**
     * Initialize Components
     */
    @PostConstruct
    public void initComponents() {
        menuAnalysis = new JMenu();
        menuConfig = new JMenu();
        menuSystem = new JMenu();
        mnuSpc = new JMenuItem();
        mnuReport = new JMenuItem();
        mnuGrr = new JMenuItem();
        mnuApp = new JMenuItem();
        mnuConfigAnalysis = new JMenuItem();
        mnuScript = new JMenuItem();
        mnuChangePwd = new JMenuItem();
        mnuSystemSettings = new JMenuItem();

        mnuLoginOut = new JMenuItem();
        final Dimension mainDialogSize = new java.awt.Dimension(86, 26);
        this.setPreferredSize(mainDialogSize);

        //init analysis menu
        menuAnalysis.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_ANALYSIS));
        mnuSpc.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_ANALYSIS_SPC));
        mnuSpc.setIcon(new ImageIcon(getClass().getResource("/icons/icon_bar.png")));
        mnuSpc.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuSpcActionPerformed(evt);
            }
        });

        menuAnalysis.add(mnuSpc);
        mnuReport.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_ANALYSIS_REPORT));
        mnuReport.setIcon(new ImageIcon(getClass().getResource("/icons/icon_report.png")));
        mnuReport.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuReportActionPerformed(evt);
            }
        });

        menuAnalysis.add(mnuReport);
        mnuGrr.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_ANALYSIS_GRR));
        mnuGrr.setIcon(new ImageIcon(getClass().getResource("/icons/icon_curve.png")));
        menuAnalysis.add(mnuGrr);
        mnuGrr.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                menuGrrActionPerformed(evt);
            }
        });


        //init config menu
        menuConfig.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_CONFIG));
        mnuConfigAnalysis.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_CONFIG_ANALYSIS));
        mnuConfigAnalysis.setIcon(new ImageIcon(getClass().getResource("/icons/icon_legend.png")));
        mnuConfigAnalysis.addActionListener(new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuSystemActionPerformed(evt);
            }
        });
        menuConfig.add(mnuConfigAnalysis);

        //init system menu
        menuSystem.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_SYSTEM));
        mnuScript.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_SYSTEM_SCRIPT));
        mnuScript.setIcon(new ImageIcon(getClass().getResource("/icons/icon_document_valid.png")));
        mnuScript.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuScriptActionPerformed(evt);
            }
        });

        mnuChangePwd.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_SYSTEM_CHANGE_PASSWORD));
        mnuChangePwd.setIcon(new ImageIcon(getClass().getResource("/icons/icon_key.png")));
        mnuChangePwd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                mnuChangePwdActionPerformed(evt);
            }
        });
        mnuSystemSettings.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_SYSTEM_SETTINGS));
        mnuSystemSettings.setIcon(new ImageIcon(getClass().getResource("/icons/icon_master_tools.png")));
        mnuSystemSettings.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuSystemConfigActionPerformed(evt);
            }
        });

        mnuLoginOut.setText(ResourceBundleUtils.getString(ResourceMessages.MENU_SYSTEM_LOGIN_OUT));
        mnuLoginOut.setIcon(new ImageIcon(getClass().getResource("/icons/icon_door_out.png")));
        mnuLoginOut.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                setLoginOutActionPerfoemed(evt);
            }
        });

        menuSystem.add(mnuSystemSettings);
        menuSystem.add(mnuScript);
        menuSystem.add(mnuChangePwd);
        menuSystem.addSeparator();
        menuSystem.add(mnuLoginOut);

        this.add(menuAnalysis);
        this.add(menuConfig);
        this.add(menuSystem);
    }

    /**
     * Make some components not visible
     */
    public void remove() {
        menuConfig.setVisible(false);
        mnuSystemSettings.setVisible(false);
        mnuScript.setVisible(false);
    }

    /**
     * Make some components visible
     */
    public void showvisiable() {
        menuConfig.setVisible(true);
        mnuSystemSettings.setVisible(true);
        mnuScript.setVisible(true);
    }

    private void mnuSpcActionPerformed(ActionEvent evt) {
            EventBusUtils.post(new PanelChangeEvent(SpcMainPanel.class, true, ResourceMessages.MENU_ANALYSIS_SPC));
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/SPC."));
    }

    private void mnuReportActionPerformed(ActionEvent evt) {
            EventBusUtils.post(new PanelChangeEvent(YieldPanel.class, true, ResourceMessages.MENU_ANALYSIS_REPORT));
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/Report/Yield."));
    }

    private void menuGrrActionPerformed(ActionEvent evt) {
            EventBusUtils.post(new PanelChangeEvent(GrrMainPanel.class, true, ResourceMessages.MENU_ANALYSIS_GRR));
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Analysis/GRR."));
    }

    private void mnuSystemActionPerformed(ActionEvent evt) {
            EventBusUtils.post(new PanelChangeEvent(AnalysisConfigPanel.class, true, ResourceMessages.MENU_CONFIG_ANALYSIS));
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Configuration/Analysis."));
    }

    private void mnuScriptActionPerformed(ActionEvent evt) {
            EventBusUtils.post(new PanelChangeEvent(ScriptTestPanel.class, true, ResourceMessages.MENU_SYSTEM_SCRIPT));
            EventBusUtils.post(new StatusBarMsgChangeEvent("In Configuration/Script Test."));
    }

    private void mnuChangePwdActionPerformed(ActionEvent evt) {
            EventBusUtils.post(new PanelChangeEvent(ChangePwdPanel.class, true, ResourceMessages.MENU_SYSTEM_CHANGE_PASSWORD));
            EventBusUtils.post(new StatusBarMsgChangeEvent("In System/Change Password."));
    }

    private void mnuSystemConfigActionPerformed(ActionEvent evt) {
            EventBusUtils.post(new PanelChangeEvent(SystemConfigPanel.class, true, ResourceMessages.MENU_SYSTEM_SETTINGS));
            EventBusUtils.post(new StatusBarMsgChangeEvent("In System/Settings."));
    }

    private void setLoginOutActionPerfoemed(ActionEvent evt) {
            int i = AppMessageFactory.showConfirmDialog("Are you sure to logout system?");
            if (i == AppMessageFactory.CANCEL_OPTION) {
                return;
            }
            if (i == AppMessageFactory.YES_OPTION) {
                EventBusUtils.post(new PanelChangeEvent(LoginPanel.class, true, ResourceMessages.MENU_SYSTEM_LOGIN_OUT));
            }
    }


    private JMenu menuAnalysis;
    private JMenu menuConfig;
    private JMenu menuSystem;

    private JMenuItem mnuSpc;
    private JMenuItem mnuReport;
    private JMenuItem mnuGrr;
    private JMenuItem mnuConfigAnalysis;
    private JMenuItem mnuScript;
    private JMenuItem mnuChangePwd;
    private JMenuItem mnuSystemSettings;

    private JMenuItem mnuApp;

    private JMenuItem mnuLoginOut;
}